//
//  EFSActivityWebController.m
//  EFSMobile
//
//  Created by CaoJun on 15/11/20.
//  Copyright © 2015年 Elephants Financial Service. All rights reserved.
//

#import "EFSActivityWebController.h"
#import "EFSWKWebViewController+CommonEvent.h"
#import "ActivityInfo.h"

#import "NSURL+YTX.h"
#import "TSMessage+YTX.h"

//#import "EFSUserManager.h"
#import "WXApi.h"

#import <QuartzCore/QuartzCore.h>

#import "EFSShareManager.h"

typedef NS_ENUM(NSUInteger, WebViewLoadStatus) {
    Normal,
    Loading,
    Error,
    Completed
};

@interface EFSActivityWebController()


@property (nonatomic, strong) ActivityInfo *activity;

@property (nonatomic, strong) UIBarButtonItem *backButton;
@property (nonatomic, strong) UIBarButtonItem *forwardButton;
@property (nonatomic, strong) UIBarButtonItem *reloadButton;

@end

@implementation EFSActivityWebController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.isShowProgress = YES;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   //@TODO("EFSAnalytics");
//    [YTXAnalytics page:OPENACC_INTENTION
//        withData:@{@"sourceType": [[YTXAnalytics sharedAnalytics] sourceType], @"sourceId": [[YTXAnalytics sharedAnalytics] sourceId]}];

    //无敌傻逼 刷URL
    if (self.activity) {
        NSURL *newUrl = [self.activity.url URLByAppendingEnvAgentAndToken];
        newUrl = [newUrl URLByAppendingQueryDictionary:@{@"appName":@"ygmobilepro",@"platform":@"ios"}];
        self.url = newUrl;
    }
    
//    self.navigationController.navigationBar.barTintColor = [UIColor navigationBarColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:
//@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

}

#pragma mark share
- (void)setupShareButton
{
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(showActionSheet:)];
    self.navigationItem.rightBarButtonItem = shareButton;
}

- (void)showActionSheet:(UIBarButtonItem*)button{
    
    @weakify(self)
    NSDictionary *shareInfo = @{
                                @"id": objectOrEmptyStr(self.activity.activityId),
                                @"type": SHARE_TYPE_ACTIVITY,
                                @"title": [self getViewTitle],
                                @"url": [NSString stringWithFormat:@"%@&os=iphone&share=true&appName=ygmobile&platform=ios",[self.activity.url absoluteString]],
                                @"content": objectOrEmptyStr(self.activity.desc),
                                @"image": objectOrEmptyStr([self.activity.shareImg absoluteString])
                                };
    [[EFSShareManager shareContentWithInfo:shareInfo] subscribeNext:^(id x) {
        @strongify(self)
        [TSMessage ytx_showNotificationInViewController:self title:x[@"message"] type:TSMessageNotificationTypeSuccess];
    } error:^(NSError *error) {
        @strongify(self)
        [TSMessage ytx_showNotificationInViewController:self title:[error.userInfo valueForKey:EFS_ERROR_MESSAGE_KEY]
                                                   type:TSMessageNotificationTypeError];
    }];
    button.enabled = YES;
}

- (NSString *)getViewTitle{
    NSString *viewTitle = self.activity.viewTitle;
    if (viewTitle) {
        objectOrEmptyStr(self.activity.viewTitle);
    }
    return  objectOrEmptyStr(self.activity.title);
    
    
}

#pragma mark activity detail
- (void)setActivity:(ActivityInfo *)activity
{
    if (!activity.activityId) {
        return;
    }
    _activity = activity;
//    _additionalParam = @{@"username": objectOrEmptyStr([EFSUserManager sharedManager].currentUser.username) ,
//                         @"usetType": objectOrEmptyStr([EFSUserManager sharedManager].currentUser.userType) ,
//                         @"activityId": objectOrEmptyStr(activity.activityId)};
    if ([self.title length] < 2){
    self.title = activity.title;
    }
    self.url = [activity.url URLByAppendingEnvAgentAndToken];
    if ([_activity.shareEnabled boolValue]) {
        _activity.url = [activity.url URLByAppendingEnvAndAgent];
//        _activity.url = [_activity.url URLByAppendingQueryDictionary:_additionalParam];
      
        if ([WXApi isWXAppInstalled]) {
              [self setupShareButton];
        }
    }
}

- (RACSignal *)showActivityByActivityId:(NSString *)activityId
{
    RACReplaySubject *subject = [RACReplaySubject subject];
    @weakify(self)
    [[ActivityInfo activityInfoById:activityId] subscribeNext:^(ActivityInfo *x) {
        @strongify(self)
        self.activity = x;
        [subject sendNext:x];
        [subject sendCompleted];
    } error:^(NSError *error) {
        //YXCH: 失败了 怎么办？
    }];
    return subject;
}

- (void)showActivity:(ActivityInfo *)activity
{
    self.activity = activity;
}



@end
